var IDMark_A = "_a";
var setting = {
			async: {
				enable: true,
				url:"/course/category/getcategories.do",
				autoParam:[],
				otherParam:{"type":1},
				type: "get",
				dataFilter: filter
			},
			view: {
				expandSpeed:"",
				addHoverDom: addHoverDom,
				removeHoverDom: removeHoverDom,
				selectedMulti: false
			},
			edit: {
				enable: false,
				editNameSelectAll: false
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				
			}
		};

function filter(treeId, parentNode, responseData){
	var nodes = new Array();
	
	if(responseData!=""){
		for ( var i = 0; i < responseData.length; i++) {
			var pid,op,chour,nocheck;
			if(responseData[i].categoryId!=null){
				pid = responseData[i].categoryId;
				op = false;
			}
			else{
				pid=0;
				op = true;
			}
			if(responseData[i].isParent){
				nocheck = true;
			}
			else{
				nocheck = false;
			}
			if(responseData[i].classHour!=null){
				chour = "(总课时:"+responseData[i].classHour+")";
			}
			else{
				chour = "";
			}
			nodes.push({
				id: responseData[i].id,
				name: responseData[i].name+chour,
				pId: pid,
				isParent: false,
				open: op,
				nocheck: nocheck,
			});
		}
	}
	return nodes;
}

var newCount = 1;
function addHoverDom(treeId, treeNode) {
	var sObj = $("#" + treeNode.tId + "_span");
	if (treeNode.editNameFlag || $("#addBtn_"+treeNode.id).length>0 || $("#removeBtn_"+treeNode.id).length>0 ) return;
	var addStr = "<span class='button add' id='addBtn_" + treeNode.id
		+ "' title='添加子课程' onfocus='this.blur();'></span>"
		+ "<span class='button remove' id='removeBtn_" + treeNode.id
		+ "' title='删除' onfocus='this.blur();'></span>";
	sObj.after(addStr);
	var btn = $("#addBtn_"+treeNode.id);
	if (btn) btn.bind("click", function(){
		
		$("#categoryId").val( treeNode.id );
		$("#tjkc").dialog("open");
		return false;
	});
	var btn = $("#removeBtn_"+treeNode.id);
	if (btn) btn.bind("click", function(){
		if( confirm("确认删除  -- " + treeNode.name + " 吗？") ){
			$.get("/course/category/delcategory.do",{id:treeNode.id},function(data){
				if(data){
					alert("删除成功！");
					var treeObj = $.fn.zTree.getZTreeObj("coursetree");
					treeObj.reAsyncChildNodes(null, "refresh");
				}
			},"json");
		}
		return false;
	});
}
function removeHoverDom(treeId, treeNode) {
	$("#addBtn_"+treeNode.id).unbind().remove();
	$("#removeBtn_"+treeNode.id).unbind().remove();
}

var classcousersetting = {
		async: {
			enable: true,
			url:"/course/category/getcoursebyclass.do",
			autoParam:[],
			otherParam:{"classId":null},
			type: "get",
			dataFilter: filter
		},
		check: {
			enable: true,
			chkStyle: "radio",
			chkboxType: {"Y":"", "N":""}
		},
		view: {
			dblClickExpand: false
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			onClick: onClick,
			onCheck: onCheck
		}
};

function onBodyDown(event) {
	if (!(event.target.id == "menuBtn" || event.target.id == "classcourseid" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
		hideMenu();
	}
}
function hideMenu() {
	$("#menuContent").fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown);
}
function onCheck(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("classcoursetree"),
	nodes = zTree.getCheckedNodes(true);
	if( nodes.length>0 ){
		$("#ccourseId").val( nodes[0].id );
		$("#classcourseid").val( nodes[0].name );
		if($("#classcourseid").next("label")!=""){
			$("#classcourseid").next("label").empty();//删除validation信息
		}
		hideMenu();
		//获取第几次上课
		$.get("/course/arrange/getcoursenum.do",{classesId:$("#classesId").val(),courseId:$("#ccourseId").val()},function(data){
			$("#number").val(data[0]);
			var len = (data[1] - data[0])*1+1;
			var cHourSum = $("#classHourSum");
			cHourSum.empty();
			for ( var i = 0; i < len; i++) {
				cHourSum.append("<option value='"+(len-i)+"'>"+(len-i)+"</option>");
			}
		},"json");
	}
}
function onClick(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("classcoursetree");
	zTree.checkNode(treeNode, true, false, true);
	return false;
}